"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，创建软链接
    2、进入交互模式，显示文件链接信息
    3、进入交互模式，取消软链接
    4、清理环境
Expect      :
    1、创建成功
    2、查看成功
    3、取消成功
    4、清理环境成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，创建软链接   expect:创建成功-----'
        self.log.info(text)

        cmd1 = f'dsscmd touch -p +data/file_src.txt'
        cmd2 = f'dsscmd touch -p +data/file_new.txt'
        res = self.node.sh(f'source {macro.DB_ENV_PATH};{cmd1};{cmd2}').result()
        self.log.info(res)
        self.assertEqual(2, res.count('Succeed to create file'), '创建文件失败')

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "ln -s +data/file_src.txt -t +data/file_new.txt\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('', res1, '创建软链接失败')

        text = '-----step2:进入交互式模式，显示文件链接信息   expect:查看成功-----'
        self.log.info(text)

        execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "readlink -p +data/file_new.txt\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd2)
        res2 = self.node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('', res2, '查看文件软链接信息失败')

        text = '-----step3:进入交互式模式，取消软链接   expect:取消成功-----'
        self.log.info(text)

        execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "unlink -p +data/file_new.txt\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd3)
        res3 = self.node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('', res3, '取消文件软链接失败')

    def tearDown(self):
        text = '-----step4:清理环境  expect:清理成功-----'
        self.log.info(text)

        clear_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'dsscmd rm -p +data/file_src.txt;' \
                    f'dsscmd rm -p +data/file_new.txt'
        self.log.info(clear_cmd)
        clear_res = self.node.sh(clear_cmd).result()
        self.log.info(clear_res)
        self.assertEqual(2, clear_res.count('Succeed to remove device'), '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
